package org.example.workbench.controller;

import org.apache.poi.hssf.eventmodel.ERFListener;
import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.User;
import org.example.workbench.domain.ContactsRemark;
import org.example.workbench.service.ContactsRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("/contactsRemark")
public class contactsRemarkCotroller {
    @Autowired
    private ContactsRemarkService contactsRemarkService;

    @RequestMapping("/deleteOneContactsRemarkById")
    public @ResponseBody Object deleteOneContactsRemarkById(String id){
        ErrorInfo errorInfo = new ErrorInfo();
        try{
            int i = contactsRemarkService.deleteOneContactsRemarkById(id);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else{
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后........");
            }
        }catch (Exception e){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后........");
        }
        return errorInfo;
    }
    @RequestMapping("/updateContactsRemarkNoteContentById")
    public @ResponseBody Object updateContactsRemarkNoteContentById(ContactsRemark contactsRemark,HttpSession session){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        contactsRemark.setEditFlag(Constant.EDIT_FLAG_YES);
        contactsRemark.setEditBy(user.getId());
        contactsRemark.setEditTime(DateFormatUtils.formatDateTime(new Date()));
        ErrorInfo errorInfo = new ErrorInfo();
        try{
            int i = contactsRemarkService.updateContactsRemarkNoteContentById(contactsRemark);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else{
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后........");
            }
        }catch (Exception e){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后........");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("errorInfo",errorInfo);
        hashMap.put("contactsRemark",contactsRemark);
        return hashMap;
    }
    @RequestMapping("/insertOneContactsRemarkByContactsRemark")
    public @ResponseBody Object insertOneContactsRemarkByContactsRemark(ContactsRemark contactsRemark, HttpSession session){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        contactsRemark.setId(UUIDUtils.getUUID());
        contactsRemark.setCreateBy(user.getId());
        contactsRemark.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        contactsRemark.setEditFlag(Constant.EDIT_FLAG_NO);
        ErrorInfo errorInfo = new ErrorInfo();
        try{
            int i = contactsRemarkService.insertOneContactsRemarkByContactsRemark(contactsRemark);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else{
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后........");
            }
        }catch (Exception e){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后........");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("errorInfo",errorInfo);
        hashMap.put("contactsRemark",contactsRemark);
        return hashMap;
    }

}
